#include "stdafx.h"
#include "./tmGridMenu.h"
#include "./tmTimer.h"
#include "./tmMsgDispatcher.h"

using namespace GLES;
typedef struct _ItemArrary_
{
	tmGridMenu::ItemType	 _type;
	char*					   _name;
}ItemArray;

ItemArray	_itemArray[ITEM_COLS * ITEM_ROWS] =
{
	{tmGridMenu::Navigation,"Navigation"},
	{tmGridMenu::Service,"Service"},
	{tmGridMenu::FaultCheck,"FaultCheck"},
	{tmGridMenu::Computer,"Computer"},
	{tmGridMenu::Dail,"Dail"},
	{tmGridMenu::Music,"Music"},
	{tmGridMenu::DouBan,"DouBan"},
	{tmGridMenu::Distributor,"Distributor"}	
};



BOOL tmGridMenu::initialise(GLES::RectI& rect)
{
	xStaticAssert(ARRAY_SIZE(_itemArray) == ARRAY_SIZE(_Items) * ARRAY_SIZE(_Items[0]),size_should_be_the_same);
	_rect = rect;
	int i = 0;
	int dltx = rect.width() / ARRAY_SIZE(_Items);
	int dlty = rect.height() / ARRAY_SIZE(_Items[0]);
	int x,y;
	for(int col = 0 ; col < ARRAY_SIZE(_Items) ; ++ col)
	{
		for(int row = 0 ; row < ARRAY_SIZE(_Items[0]) ; ++ row)
		{
			i = ARRAY_SIZE(_Items) * col + row;
			x = rect.x1 + dltx * col;
			y = rect.y1 + dlty * row;
			E_Return(_Items[col][row].createFrom("icon_4s_normal.pvr",GLES::PointI(x,y)));
		}
	}
	return true;
}

VOID tmGridMenu::draw( GLES::Painter* painter )
{
	for(int col = 0 ; col < ARRAY_SIZE(_Items) ; ++ col)
	{
		for(int row = 0 ; row < ARRAY_SIZE(_Items[0]) ; ++ row)
		{
			_Items[col][row]._label.draw(painter);
		}
	}
}

VOID tmGridMenu::onTimer( tmTimer* timer )
{
	if(_anim.getIsMoving())
	{
		GLES::Singleton<tmMsgDispatcher>::GetSingleton()->invalidateRect(_rect,true);
		int dist = _anim.getMovingDistance();
		RectI rect = _rect;
		int i;
		for(int col = 0 ; col < ARRAY_SIZE(_Items) ; ++ col)
		{
			for(int row = 0 ; row < ARRAY_SIZE(_Items[0]) ; ++ row)
			{
				GLES::PointI p = _Items[col][row].getPosition();
				p.x = dist + p.x;
				_Items[col][row].setPosition(p);
			}
		}
	}
}

VOID tmGridMenu::onPressed( int x,int y )
{
	_presY = y;
	_presX = x;
}

VOID tmGridMenu::onDragged( int x,int y )
{
	RectI rect = _rect;
	int dltx = x - _presX;
	int dlty = y - _presY;
	_presX = x;
	_presY = y;
	int i;
	for(int col = 0 ; col < ARRAY_SIZE(_Items) ; ++ col)
	{
		for(int row = 0 ; row < ARRAY_SIZE(_Items[0]) ; ++ row)
		{
			GLES::PointI p = _Items[col][row].getPosition();
			p.x += dltx;
			p.y += dlty;
			_Items[col][row].setPosition(p);
			xInfor2("x:%d,y:%d",dltx,dlty);
		}
	}
}

VOID tmGridMenu::onDragRelease( int x,int y )
{

}

VOID tmGridMenu::onPressRelease( int x,int y )
{

}

VOID tmGridMenu::onInput( int x,int y,InputType type )
{
	tmControl::onInput(x,y,type);
	_anim.onInput(x,type);
}

BOOL tmGridMenu::tmGridMenuItem::createFrom( char* name,GLES::PointI& pos )
{
	return _label.createFrom(name,pos);
}

VOID tmGridMenu::tmGridMenuItem::setPosition( PointI& pos )
{
	_label.setPoint(pos);
}

VOID tmGridMenu::tmGridMenuItem::movePosition( Vector2I& vec )
{
	_label.movePosition(vec);
}

const GLES::PointI& tmGridMenu::tmGridMenuItem::getPosition()
{
	return _label.getPoint();
}